﻿using System;
using Resources;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Announcement
{
    public partial class Update : BasePage_BackEnd
    {
        //todo lijun
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }
        }

        public override void InitControlResource()
        {
            this.rtbnChLanguage.Text = NameResource.Language_CN_Label;
            this.rtbnEnLanguage.Text = NameResource.Language_EN_Label;
            this.btnAnnouncementSubmit.Text = ManageResource.mgr_btnOK;
            this.btnAnnouncementCancel.Text = ManageResource.mgr_btnCancel;
            this.lblLanguage.Text = NameResource.Language;
            this.lblSubject.Text = NameResource.Announcement_Subject;
            this.rfvtxtSubject.ErrorMessage =  NameResource.Announcement_Subject + CommonResource.NotNull;
        }

        protected void InitData()
        {
            lblNullError.Text = string.Empty;
            rtbnEnLanguage.Checked = false;
            rtbnChLanguage.Checked = false;
            if (string.Equals(GetQueryString("pageStatus"), "edit", StringComparison.CurrentCultureIgnoreCase))
            {
                var currentAnnouncement = AnnouncementPresent.GetById(Id);
                if (currentAnnouncement != null)
                {
                    string str1 = currentAnnouncement.Language;
                    if ((currentAnnouncement.Language).IndexOf(Language.EN.ToString()) != -1)
                    {
                        rtbnEnLanguage.Checked = true;
                    }
                    if ((currentAnnouncement.Language).IndexOf(Language.CN.ToString()) != -1)
                    {
                        rtbnChLanguage.Checked = true;
                    }
                    txtSubject.Text = Server.HtmlDecode(currentAnnouncement.Subject);
                    fckBody.Value = Server.HtmlDecode(currentAnnouncement.Body);
                    PageStatus = Utility.PageStatus.Edit;
                }
                else { this.phMain.Visible = false; }
            }
            else if (string.Equals(GetQueryString("pageStatus"), "new", StringComparison.CurrentCultureIgnoreCase))
            {
                rtbnChLanguage.Checked = true;
                txtSubject.Text = string.Empty;
                fckBody.Value = string.Empty;
                PageStatus = Utility.PageStatus.New;
            }
            else
            {
                this.phMain.Visible = false;
            }
        }

        protected void btnAnnouncementSubmit_Click(object sender, EventArgs e)
        {
            int i = 0;
            lblNullError.Text = string.Empty;
            lblfckBodyError.Text = string.Empty;
            var fckbody = Server.HtmlEncode(this.fckBody.Value);
            if (fckbody.Length > 8000)
            {
                lblfckBodyError.Text = string.Format(NameResource.OverMaxLength,"8000");
                i++;
            }
            if (fckbody.Length == 0)
            {
                lblfckBodyError.Text =  NameResource.Announcement_Body + CommonResource.NotNull;
                i++;
            }
            if (!rtbnChLanguage.Checked && !rtbnEnLanguage.Checked)
            {
                lblNullError.Text =NameResource.Language + CommonResource.NotNull;
                i++;
            }
            if (i > 0)
                return;
            var language = string.Empty;
            language = rtbnChLanguage.Checked ? Language.CN.ToString() + language : language;
            language = rtbnEnLanguage.Checked ? Language.EN.ToString() + language : language;
            switch (PageStatus)
            {
                case Utility.PageStatus.Edit:
                    var updateAnnouncement = AnnouncementPresent.GetById(Id);
                    updateAnnouncement.Subject = Server.HtmlEncode(txtSubject.Text);
                    updateAnnouncement.Body = Server.HtmlEncode(this.fckBody.Value);
                    updateAnnouncement.Language = language;
                    updateAnnouncement.CreateBy = CurrentUserId;//CurrentUserId;
                    AnnouncementPresent.Update(updateAnnouncement);
                    break;
                case Utility.PageStatus.New:
                    Model.Announcement announcement = new Model.Announcement()
                    {
                        Language = language,
                        Subject = Server.HtmlEncode(txtSubject.Text),
                        Body = Server.HtmlEncode(this.fckBody.Value),
                        CreateBy = CurrentUserId,//CurrentUserId,
                        CreateTime = DateTime.Now,
                    };
                    AnnouncementPresent.Create(announcement);
                    break;
            }
            Response.Redirect("/Announcement/View.aspx");
        }

        protected void btnAnnouncementCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Announcement/View.aspx");
        }

    }
}